package com.core.cloudcommon.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author =====Lqj=====
 * @className IdUtils
 * @description ID生成工具类 - 提供静态方法调用
 * @create 2024/12/17 10:30
 **/
@Component
public class IdUtils {

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    private static SnowflakeIdGenerator staticSnowflakeIdGenerator;

    @PostConstruct
    public void init() {
        staticSnowflakeIdGenerator = snowflakeIdGenerator;
    }

    // ========================================
    // 系统模块ID生成
    // ========================================

    /**
     * 生成系统用户ID
     */
    public static long generateSystemUserId() {
        return staticSnowflakeIdGenerator.generateSystemUserId();
    }

    /**
     * 生成系统部门ID
     */
    public static long generateSystemDepartId() {
        return staticSnowflakeIdGenerator.generateSystemDepartId();
    }

    /**
     * 生成系统登录日志ID
     */
    public static long generateSystemLoginId() {
        return staticSnowflakeIdGenerator.generateSystemLoginId();
    }

    /**
     * 生成系统日志ID
     */
    public static long generateSystemLogId() {
        return staticSnowflakeIdGenerator.generateSystemLogId();
    }

    /**
     * 生成设备管理ID
     */
    public static long generateSystemDeviceId() {
        return staticSnowflakeIdGenerator.generateSystemDeviceId();
    }

    /**
     * 生成安全管理ID
     */
    public static long generateSystemSecurityId() {
        return staticSnowflakeIdGenerator.generateSystemSecurityId();
    }

    // ========================================
    // 工单模块ID生成
    // ========================================

    /**
     * 生成12345工单ID
     */
    public static long generateSheetMayorId() {
        return staticSnowflakeIdGenerator.generateSheetMayorId();
    }

    /**
     * 生成网络工单ID
     */
    public static long generateSheetNetworkId() {
        return staticSnowflakeIdGenerator.generateSheetNetworkId();
    }

    /**
     * 生成信访工单ID
     */
    public static long generateSheetLettersId() {
        return staticSnowflakeIdGenerator.generateSheetLettersId();
    }

    /**
     * 生成工单流程ID
     */
    public static long generateSheetProcessId() {
        return staticSnowflakeIdGenerator.generateSheetProcessId();
    }

    /**
     * 生成工单附件ID
     */
    public static long generateSheetAttachmentId() {
        return staticSnowflakeIdGenerator.generateSheetAttachmentId();
    }

    // ========================================
    // 网关模块ID生成
    // ========================================

    /**
     * 生成网关认证ID
     */
    public static long generateGatewayAuthId() {
        return staticSnowflakeIdGenerator.generateGatewayAuthId();
    }

    // ========================================
    // 开放接口模块ID生成
    // ========================================

    /**
     * 生成开放接口ID
     */
    public static long generateOpenApiId() {
        return staticSnowflakeIdGenerator.generateOpenApiId();
    }

    // ========================================
    // 通用方法
    // ========================================

    /**
     * 根据模块代码生成ID
     */
    public static long generateId(SnowflakeIdGenerator.ModuleCode moduleCode) {
        return staticSnowflakeIdGenerator.generateId(moduleCode);
    }

    /**
     * 解析雪花ID
     */
    public static SnowflakeIdGenerator.SnowflakeInfo parseSnowflakeId(long id) {
        return staticSnowflakeIdGenerator.parseSnowflakeId(id);
    }

    /**
     * 获取模块统计信息
     */
    public static String getModuleStatistics() {
        return staticSnowflakeIdGenerator.getModuleStatistics();
    }

    // ========================================
    // 便捷方法 - 根据业务场景快速生成ID
    // ========================================

    /**
     * 生成用户相关ID（用户、部门等）
     */
    public static long generateUserRelatedId() {
        return generateSystemUserId();
    }

    /**
     * 生成工单相关ID（根据工单类型自动选择）
     */
    public static long generateSheetId(Integer sheetType) {
        if (sheetType == null) {
            return generateSheetMayorId(); // 默认12345工单
        }
        
        switch (sheetType) {
            case 1001: // 12345工单
                return generateSheetMayorId();
            case 2001: // 网络工单
                return generateSheetNetworkId();
            case 3001: // 信访工单
                return generateSheetLettersId();
            default:
                return generateSheetMayorId();
        }
    }

    /**
     * 生成日志相关ID
     */
    public static long generateLogId() {
        return generateSystemLogId();
    }

    /**
     * 生成安全相关ID
     */
    public static long generateSecurityId() {
        return generateSystemSecurityId();
    }

    // ========================================
    // 批量生成方法
    // ========================================

    /**
     * 批量生成指定模块的ID
     */
    public static long[] generateIds(SnowflakeIdGenerator.ModuleCode moduleCode, int count) {
        if (count <= 0) {
            return new long[0];
        }
        
        long[] ids = new long[count];
        for (int i = 0; i < count; i++) {
            ids[i] = generateId(moduleCode);
        }
        return ids;
    }

    /**
     * 批量生成系统用户ID
     */
    public static long[] generateSystemUserIds(int count) {
        return generateIds(SnowflakeIdGenerator.ModuleCode.SYSTEM_USER, count);
    }

    /**
     * 批量生成工单ID
     */
    public static long[] generateSheetIds(int count, Integer sheetType) {
        SnowflakeIdGenerator.ModuleCode moduleCode;
        if (sheetType == null || sheetType == 1001) {
            moduleCode = SnowflakeIdGenerator.ModuleCode.SHEET_MAYOR;
        } else if (sheetType == 2001) {
            moduleCode = SnowflakeIdGenerator.ModuleCode.SHEET_NETWORK;
        } else if (sheetType == 3001) {
            moduleCode = SnowflakeIdGenerator.ModuleCode.SHEET_LETTERS;
        } else {
            moduleCode = SnowflakeIdGenerator.ModuleCode.SHEET_MAYOR;
        }
        
        return generateIds(moduleCode, count);
    }

    // ========================================
    // 验证方法
    // ========================================

    /**
     * 验证ID是否为有效的雪花ID
     */
    public static boolean isValidSnowflakeId(long id) {
        try {
            SnowflakeIdGenerator.SnowflakeInfo info = parseSnowflakeId(id);
            return info.getTimestamp() > 0 && info.getModuleCode() != null;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 验证ID是否属于指定模块
     */
    public static boolean isModuleId(long id, SnowflakeIdGenerator.ModuleCode moduleCode) {
        try {
            SnowflakeIdGenerator.SnowflakeInfo info = parseSnowflakeId(id);
            return info.getModuleCode() == moduleCode;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 获取ID所属的模块
     */
    public static SnowflakeIdGenerator.ModuleCode getIdModule(long id) {
        try {
            SnowflakeIdGenerator.SnowflakeInfo info = parseSnowflakeId(id);
            return info.getModuleCode();
        } catch (Exception e) {
            return null;
        }
    }
}
